package com.itliang.web.controller.system;

import com.itliang.annotation.Log;
import com.itliang.core.model.DeleteRequest;
import com.itliang.core.model.IdRequest;
import com.itliang.core.model.dto.system.sysmenu.AssignMenuRequest;
import com.itliang.core.model.dto.system.sysmenu.SysMenuAddRequest;
import com.itliang.core.model.dto.system.sysmenu.SysMenuUpdateRequest;
import com.itliang.core.model.entity.system.SysMenu;
import com.itliang.core.result.BaseResponse;
import com.itliang.core.result.CodeEnum;
import com.itliang.core.result.ResultUtils;
import com.itliang.enums.BusinessType;
import com.itliang.exception.BusinessException;
import com.itliang.service.system.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : liangzhuoyuan
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Resource
    SysMenuService sysMenuService;

    /**
     * 根据角色获取菜单
     *
     * @param idRequest
     * @return
     */
    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/toAssign")
    public BaseResponse toAssign(IdRequest idRequest) {
        // TODO 参数校验
        List<SysMenu> list = sysMenuService.getSysMenuByRoleId(idRequest.getId());
        return ResultUtils.success(list);
    }

    /**
     * 给角色分配权限
     *
     * @param assignMenuRequest
     * @return
     */
    @Log(title = "菜单管理", businessType = BusinessType.ASSIGN)
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public BaseResponse doAssign(@RequestBody AssignMenuRequest assignMenuRequest) {
        sysMenuService.doAssign(assignMenuRequest);
        return ResultUtils.success();
    }

    // region 增删改查

    /**
     * 创建菜单
     *
     * @param sysMenuAddRequest
     * @return
     */
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public BaseResponse<Long> addMenu(@RequestBody SysMenuAddRequest sysMenuAddRequest) {
        if (sysMenuAddRequest == null) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuAddRequest, sysMenu);
        boolean result = sysMenuService.save(sysMenu);
        if (!result) {
            throw new BusinessException(CodeEnum.OPERATION_ERROR);
        }
        return ResultUtils.success(sysMenu.getId());
    }

    /**
     * 删除菜单
     *
     * @param deleteRequest
     * @return
     */
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteMenu(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        boolean b = sysMenuService.deleteMenuById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新菜单
     *
     * @param sysMenuUpdateRequest
     * @return
     */
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public BaseResponse<Boolean> updateMenu(@RequestBody SysMenuUpdateRequest sysMenuUpdateRequest) {
        if (sysMenuUpdateRequest == null || sysMenuUpdateRequest.getId() == null) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuUpdateRequest, sysMenu);
        boolean result = sysMenuService.updateById(sysMenu);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取菜单
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<SysMenu> getMenuById(int id) {
        if (id <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        SysMenu sysMenu = sysMenuService.getById(id);
        return ResultUtils.success(sysMenu);
    }

    /**
     * 菜单列表（树形）
     *
     * @return
     */
    @GetMapping("/list/nodes")
    public BaseResponse<List<SysMenu>> listMenuNodes() {
        List<SysMenu> list = sysMenuService.listMenuNodes();
        return ResultUtils.success(list);
    }

    // endregion
}
